package com.capacitydorm.server.service;

import com.capacitydorm.server.common.util.StudentsUtils;
import com.capacitydorm.server.dao.DormMapper;
import com.capacitydorm.server.dao.StudentMapper;
import com.capacitydorm.server.domain.Student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StudentService {

    @Autowired

    private StudentMapper studentMapper;
    @Autowired

    private DormMapper   dormMapper;

    @Autowired
    private   StudentsUtils   studentsUtils;
    /**
     * 分页查询
     *
     * @param page
     * @param rows
     * @return
     */
    public PageInfo<Student> pageFindStudent(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<Student> students = studentMapper.findStudents();
        return new PageInfo<Student>(students);
    }

    /**
     * 删除学生
     *
     * @param id
     */
    public void deleteStudent(String id) {
        studentMapper.deleteStudent(id);
    }

    /**
     * 根据ID修改学生状态
     *
     * @param student
     */
    public void editStudentState(Student student) {

        Student byId = findById(student.getId() + "");
        if (0 == student.getState()) {
            byId.setLastout(new Date());
            byId.setState(student.getState());
            studentMapper.editStudent(byId);
        } else {
            byId.setLastin(new Date());
            byId.setState(student.getState());
            studentMapper.editStudent(byId);
        }

    }

    /**
     * 学生信息修改
     * @param student
     */
     public    void  editStudent(Student  student){
           studentMapper.updateByPrimaryKey(student);
     }

    /**
     * 根据ID查询学生
     *
     * @param id
     * @return
     */
    public Student findById(String id) {

        return studentMapper.findById(id);

    }

    /**
     * 添加学生
     *
     * @param student
     */
    public void addStudent(Student student) {
       studentMapper.insert(student);
    }

    /**
     * 根据多条件查询学生
     *
     * @param page
     * @param rows
     * @param student
     * @return
     */
    public PageInfo<Student> findByStudent(Integer page, Integer rows, Student student) {
        System.out.println(student.getState());
        PageHelper.startPage(page, rows);
        if (student.getName() != null) {
            student.setName("%" + student.getName() + "%");
        }
        if (student.getReason() != null) {
            student.setName("%" + student.getReason() + "%");
        }
        List<Student> list = studentMapper.findStudent(student);

        return new PageInfo<>(list);
    }

    /**
     * 根据姓名模糊查询
     *
     * @param page
     * @param rows
     * @param name
     * @return
     */
    public PageInfo<Student> findStudentByName(Integer page, Integer rows, String name) {
        PageHelper.startPage(page, rows);
        if (name != null) {
            name = "%" + name + "%";
        }
        List<Student> list = studentMapper.findStudentByName(name);

        return new PageInfo<>(list);
    }

    /**
     * 定时修改所有学生的状态
     */
    public void changeAllStudent() {
        studentMapper.changeAllStudent();
    }
}
